package com.ant.storage.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.DepDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.pojo.Department;
import com.ant.storage.pojo.DepartmentMsg;
import com.ant.storage.service.IDepartmentMsgService;
import com.ant.storage.service.IDepartmentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/department")
@Api(tags = "部门接口")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IDepartmentMsgService departmentMsgService;

//    @ApiOperation(value = "获取部门信息列表")
//    @GetMapping("/getDepartmentList")
//    public ResultVo getDepartmentList()
//    {
//        List<DepartmentMsg> list = departmentMsgService.list();
////        获取根节点
//        List<DepartmentMsg> Departments = list.stream()
//                .filter(e -> e.getParentId()==0)
//                .map(e -> {
//                    e.setChildren(getChildrens(e, list));
//                    return e;
//                }).collect(Collectors.toList());
//
//
//        return ResultVo.success("获取成功",Departments);
//    }

    /**
     * 递归查询子部门，并设置子部门
     * @param e
     * @param list
     * @return
     */
    private List<DepartmentMsg> getChildrens(DepartmentMsg e, List<DepartmentMsg> list) {
        List<DepartmentMsg> children = list.stream()
                .filter(m -> Objects.equals(e.getId(), m.getParentId()))
                .map(m -> {
                    m.setChildren(getChildrens(m, list));
                    return m;
                }).collect(Collectors.toList());
        return children;
    }

    @ApiOperation(value = "保存或者修改")
    @PostMapping("/save")
    public ResultVo save(@RequestBody DepartmentMsg msg)
    {
        if(msg.getId()!=null){
            departmentMsgService.updateById(msg);
        }else{
            departmentMsgService.save(msg);
        }
        return ResultVo.success("操作成功");
    }

    @ApiOperation(value = "获取部门信息列表")
    @GetMapping("/getDepartmentList")
    public ResultVo getDepartmentList(DepDto dto)
    {
        List<Department> list;
        QueryWrapper<Department> wrapper=new QueryWrapper();
        if(StringUtils.isNotEmpty(dto.getDepName())){
            wrapper.like("cashbook_name",dto.getDepName());
            list=departmentService.list(wrapper);
        }else{
            list = departmentService.list(wrapper);
        }

        return ResultVo.success("获取成功",list);
    }


    @ApiOperation(value = "获取部门信息列表")
    @GetMapping("/getDepartmentListById/{did}")
    public ResultVo getDepartmentListById(@PathVariable("did") Integer did)
    {
        if(did==null){
            throw  new AntException(ResultCode.FAILURE.getResultCode(),"id不能为空");
        }
        return ResultVo.success("获取成功",departmentService.getDepartmentListById(did));

    }


    @ApiOperation(value = "获取部门信息列表")
    @GetMapping("/getDepartmentTreeList")
    public ResultVo getDepartmentTreeList()
    {
        List<Department> list = departmentService.list();
        //        获取根节点
        List<Department> Departments = list.stream().filter(e -> e.getParentId()==1)
                .map(e -> {
                    e.setChildren(getTreeChildren(e, list));
                    return e;
                }).collect(Collectors.toList());
        return ResultVo.success("获取成功",Departments);
    }

    private List<Department> getTreeChildren(Department e, List<Department> list) {
        List<Department> children = list.stream()
                .filter(m -> Objects.equals(e.getDepartmentId(), m.getParentId()))
                .map(m -> {
                    m.setChildren(getTreeChildren(m, list));
                    return m;
                }).collect(Collectors.toList());
        return children;
    }

    @ApiOperation(value = "保存或者修改")
    @PostMapping("/saveDep")
    public ResultVo saveDep(@RequestBody Department msg)
    {
        if(msg.getId()!=null){
            departmentService.updateById(msg);
        }else{
            departmentService.save(msg);
            departmentService.update(new UpdateWrapper<Department>()
                    .set("department_id",msg.getId()).eq("id",msg.getId()));
        }
        return ResultVo.success("操作成功");
    }

    @ApiOperation(value = "获取上级部门")
    @GetMapping("/getDep/{depId}")
    public ResultVo getDep(@PathVariable("depId") Integer depId)
    {
        return ResultVo.success("操作成功",departmentService.getDepName(depId));
    }

    @ApiOperation(value = "删除部门")
    @PostMapping("/depDel/{id}")
    public ResultVo depDel(@PathVariable("id") Integer id)
    {
        return ResultVo.success("操作成功",departmentService.removeById(id));
    }
}
